package com.tangyuan.recommend;

import cn.hutool.core.collection.CollectionUtil;
import com.tangyuan.feignapis.user.IUserActionFeignClient;
import com.tangyuan.model.Movies;
import com.tangyuan.model.UserAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class BashRecommend {

    @Autowired
    private IUserActionFeignClient userActionFeignClient;

    /**
     *不重复推荐处理 得到具体的电影列表
     * @param userId
     * @param movies
     * @return
     */
    public List<Movies> getRecommendMovieList(Long userId, List<Movies> movies){
        //不重复推荐处理
        List<UserAction> list = userActionFeignClient.selectUserActionList(userId, null);
        for (UserAction action : list) {
            Long movieId = action.getMovieId();
            for (Movies movie : movies) {
                Long mymovie = movie.getId();
                if (Objects.equals(mymovie, movieId)) {
                    movies.remove(movie);
                    break;
                }
            }
        }
        return movies;
    }

    /**
     *不重复推荐处理 得到具体的电影id的列表
     * @param userId
     * @param moviesIds
     * @return
     */
    public List<Long> getRecommendMovieIdList(Long userId, List<Long> moviesIds){
        //不重复推荐处理
        List<UserAction> userActions = userActionFeignClient.selectUserActionList(userId, null);
        if(CollectionUtil.isEmpty(userActions)){
            return moviesIds;
        }
        List<Long> list = userActions.stream().map(UserAction::getMovieId).collect(Collectors.toList());
        for (Long id : moviesIds) {
            if (list.contains(id)) {
                moviesIds.remove(id);
                break;
            }
        }
        return moviesIds;
    }
}
